//
//  RHPlantInfoCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/26.
//

#import "RHPlantInfoCell.h"

@interface RHPlantInfoCell()

//创建时间
@property (nonatomic ,strong) QMUILabel *dateLab;
//位置
@property (nonatomic ,strong) QMUIButton *addressBtn;
//电站图
@property (nonatomic ,strong) UIImageView *plantImageV;
//今日发电量
@property (nonatomic ,strong) QMUILabel *dPowerGeneratLab;
//月发电量
@property (nonatomic ,strong) QMUILabel *mPowerGeneratLab;
//累计发电量
@property (nonatomic ,strong) QMUILabel *tPowerGeneratLab;



//装机容量
@property (nonatomic ,strong) QMUILabel *capacityLab;
//额定功率
@property (nonatomic ,strong) QMUILabel *ratePowerLab;
//月收益
@property (nonatomic ,strong) QMUILabel *monthIncomeLab;
//月发电量
@property (nonatomic ,strong) QMUILabel *monthPowGenLab;
//累计收益
@property (nonatomic ,strong) QMUILabel *totalIncomLab;
//累计发电量
@property (nonatomic ,strong) QMUILabel *totalgenLab;
@end

@implementation RHPlantInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,0, 0, 0));
    }];
    
    _plantImageV = [[UIImageView alloc] init];
    _plantImageV.contentMode = UIViewContentModeScaleAspectFill;
    _plantImageV.clipsToBounds = YES;
    _plantImageV.backgroundColor = kColorRGBA(240, 240, 240, 0.8);
    [_plantImageV setImage:[UIImage imageNamed:@"plant_default_pic_01"]];
    [bgView addSubview:_plantImageV];
    
    [_plantImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_offset(0);
        make.height.equalTo(weakSelf.plantImageV.mas_width).multipliedBy(214/375.0);
    }];
   
    UIView *coverView = [[UIView alloc]init];
    coverView.backgroundColor = kColorRGBA(0, 0, 0, 0.3);
    [_plantImageV addSubview:coverView];
    [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    QMUIButton *adressBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [adressBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [adressBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [adressBtn.titleLabel setFont:kSYSTEMFONT(12)];
    [adressBtn setImagePosition:QMUIButtonImagePositionLeft];
    [adressBtn setSpacingBetweenImageAndTitle:5];
    [_plantImageV addSubview:adressBtn];
    self.addressBtn = adressBtn;
    
    [adressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.bottom.mas_offset(-15);
        make.width.lessThanOrEqualTo(@(kUI_WINDOW_W-40));
    }];
    
    UIView *contentView = kView;
    contentView.backgroundColor = kCOLOR_HEX_A(@"#FFFFFF", 0.2);
    contentView.layer.cornerRadius = 8;
    contentView.layer.masksToBounds = YES;
    [coverView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(50);
        make.right.mas_offset(-50);
        make.centerY.mas_offset(0);
        make.height.offset(95);
    }];
    
    
    //今日发电量  今日收益
    _dPowerGeneratLab = [[QMUILabel alloc]init];
    UIView *item_today = [self createItemViewWithTitle:[NSString stringWithFormat:@"%@ (kWh)",@"今日发电量".rh_localized] Image:@"plant_day_icon_01" lab:_dPowerGeneratLab btnFont:kSYSTEMFONT(12) labFont:kBOLDSYSTEMFONT(28)];
    [contentView addSubview:item_today];
    
    _mPowerGeneratLab = [[QMUILabel alloc]init];
    UIView *item_month = [self createItemViewWithTitle:[NSString stringWithFormat:@"%@ ($)",@"今日收益".rh_localized] Image:@"plant_month_icon" lab:_mPowerGeneratLab btnFont:kSYSTEMFONT(12) labFont:kBOLDSYSTEMFONT(28)];
    [contentView addSubview:item_month];
    

    
    [item_today mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.centerY.mas_offset(0);
        make.width.offset((kUI_WINDOW_W-100-10)/2);
    }];
    
    [item_month mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(item_today.mas_right).offset(10);
        make.centerY.mas_offset(0);
        make.width.equalTo(item_today);
    }];
    
  
    
    //装机容量  额定功率
   
    
    //-----------------------
    __block UIView *lastView;
    __block CGFloat itemW = (kUI_WINDOW_W-30-10)/2;
    NSArray *titles = @[@"装机容量".rh_localized,@"当前功率".rh_localized];
    NSArray *images = @[@"plant_info_cap",@"plant_info_pow"];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [weakSelf itemViewWithIndex:idx title:titles[idx] image:images[idx]];
        [bgView addSubview:itemView];
        if(idx == 0){
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.equalTo(weakSelf.plantImageV.mas_bottom).offset(15);
                make.width.offset(itemW);
                make.height.offset(kScaleW(65));
            }];
        }else{
            if(idx%2==0){//换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(15);
                    make.top.equalTo(lastView.mas_bottom).offset(10);
                    make.size.equalTo(lastView);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(10);
                    make.centerY.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
            }
        }
                
        lastView = itemView;
    }];
    
    //封底
//    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.mas_offset(0);
//    }];
//    
    
    //当月数据
    self.monthIncomeLab = [[QMUILabel alloc]init];
    self.monthPowGenLab = [[QMUILabel alloc]init];
    self.monthIncomeLab.text = @"--";
    self.monthPowGenLab.text = @"--";
    
    UIView *monthView = [self itemWithTypeTitle:@"当月数据".rh_localized subTitle1:@"月发电量".rh_localized lab1:self.monthPowGenLab subTitle2:@"月收益".rh_localized lab2:self.monthIncomeLab icon:@"plant_info_month"];
    [bgView addSubview:monthView];
    [monthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(lastView.mas_bottom).offset(10);
    }];
    
    //累计数据
    self.totalIncomLab = [[QMUILabel alloc]init];
    self.totalgenLab = [[QMUILabel alloc]init];
    self.totalIncomLab.text = @"--";
    self.totalgenLab.text = @"--";
    UIView *totalView = [self itemWithTypeTitle:@"累计数据".rh_localized subTitle1:@"累计发电量".rh_localized lab1:self.totalgenLab subTitle2:@"累计收益".rh_localized lab2:self.totalIncomLab icon:@"plant_info_total"];
    [bgView addSubview:totalView];
    [totalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(monthView);
        make.right.equalTo(monthView);
        make.top.equalTo(monthView.mas_bottom).offset(10);
        make.bottom.mas_offset(-10);
    }];
}

-(void)setImgUrl:(NSString *)imgUrl
{
    [self.plantImageV sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"plant_default_pic_01"]];
}

-(void)setModel:(RHPlantDetailModel *)model
{
    _model = model;
    [self.addressBtn setTitle:ValidNullDataToString(model.address, @"--") forState:UIControlStateNormal];
    self.dateLab.text = model.createTime;
    //保留两位小数
    //今日发电量
    [self.dPowerGeneratLab setText:[NSString stringWithFormat:@"%@",[NSString stringAfterPoint:ValidNullDataToString(model.todayGeneration,@"--")]]];
    //今日收益
    [self.mPowerGeneratLab setText:[NSString stringWithFormat:@"%@",[NSString stringAfterPoint:ValidNullDataToString(model.todayRevenue,@"--")]]];
    
    
    //装机容量
    [self.capacityLab setText:[NSString stringWithFormat:@"%@(kWh)",[NSDecimalNumber decimalStringWithDoubleString:model.accumulatedInstalledCapacity nullStr:@"--"]]];
    self.capacityLab.attributedText = [self attrStringWithContent:self.capacityLab.text attrKey: @"(kWh)"];
    //额定功率
    NSString *value_all = [NSDecimalNumber decimalStringWithDoubleString:model.currentPower nullStr:@"--"];
    [self.ratePowerLab setText:[NSString stringWithFormat:@"%@(kW)",value_all]];
    self.ratePowerLab.attributedText = [self attrStringWithContent:self.ratePowerLab.text attrKey:@"(kW)"];
   //月收益
    NSString *monthIncomeValue = [NSDecimalNumber decimalStringWithDoubleString:model.monthRevenue nullStr:@"--"];
    [self.monthIncomeLab setText:[NSString stringWithFormat:@"%@($)",monthIncomeValue]];
    self.monthIncomeLab.attributedText = [self attrStringWithContent:self.monthIncomeLab.text attrKey:@"($)"];
    //月发电量
    NSString *monthGenValue = [NSDecimalNumber decimalStringWithDoubleString:model.monthGeneration nullStr:@"--"];
    [self.monthPowGenLab setText:[NSString stringWithFormat:@"%@(kWh)",monthGenValue]];
    self.monthPowGenLab.attributedText = [self attrStringWithContent:self.monthPowGenLab.text attrKey:@"(kWh)"];
    //累计收益
    NSString *totalIncomeValue = [NSDecimalNumber decimalStringWithDoubleString:model.accumulatedRevenue nullStr:@"--"];
    [self.totalIncomLab setText:[NSString stringWithFormat:@"%@($)",totalIncomeValue]];
    self.totalIncomLab.attributedText = [self attrStringWithContent:self.totalIncomLab.text attrKey:@"($)"];
    //累计发电量
    NSString *totalGenValue = [NSDecimalNumber decimalStringWithDoubleString:model.accumulatedGeneration nullStr:@"--"];
    [self.totalgenLab setText:[NSString stringWithFormat:@"%@(kWh)",totalGenValue]];
    self.totalgenLab.attributedText = [self attrStringWithContent:self.totalgenLab.text attrKey:@"(kWh)"];
}

//上面的电站发电量统计(日发电量,今日收益)
-(UIView *)createItemViewWithTitle:(NSString *)title Image:(NSString*)imageStr lab:(QMUILabel*)lab btnFont:(UIFont*)btn_font labFont:(UIFont*)lab_font
{
    UIView *bgView = kView;
    
    QMUILabel *titleLab = [QMUILabel labelWithFont:btn_font textColor:kCOLOR_HEX(@"FFFFFF") textAligment:NSTextAlignmentCenter];
    titleLab.text = title;
    titleLab.numberOfLines = 2;
    titleLab.adjustsFontSizeToFitWidth = YES;
    titleLab.minimumScaleFactor = 0.6;
    [bgView addSubview:titleLab];
    
   
    [lab setTextAlignment:NSTextAlignmentCenter];
    lab.text = @"--";
    [lab setFont:lab_font];
    [lab setTextColor:[UIColor whiteColor]];
    [bgView addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
        
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab.mas_bottom).offset(5);
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    
    
    return bgView;
}
//功率统计(装机容量,额定功率)
-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr
{
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.mas_offset(15);
        make.bottom.mas_offset(-30);
        
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 2;
    titleLab.adjustsFontSizeToFitWidth = YES;
    titleLab.minimumScaleFactor = 0.6;
    titleLab.text = [NSString stringWithFormat:@"%@",title];
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    titleLab.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 200 + idx;
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = [UIFont boldSystemFontOfSize:16];
    [bgView addSubview:contentLab];
    
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(12);
        make.right.mas_offset(-3);
        make.top.equalTo(img);
       
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentLab);
        make.top.equalTo(contentLab.mas_bottom).mas_offset(5);
    }];
    
    
    
    if(idx == 0){
        self.capacityLab = contentLab;
    }else if (idx == 1){
        self.ratePowerLab = contentLab;
    }
    return bgView;
}


//当月数据  累计数据

-(UIView*)itemWithTypeTitle:(NSString*)title  subTitle1:(NSString *)title1 lab1:(QMUILabel*)lab1 subTitle2:(NSString*)title2 lab2:(QMUILabel*)lab2 icon:(NSString*)icon
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    
    //左上角图标
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    //左上角标题
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView addSubview:titleLab];
    titleLab.text = title;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    CGFloat itemWidth = bgWidth / 2;
    //左边第一个
    
    UIView *itemView_1 = kView;
    [itemBgView addSubview:itemView_1];
    QMUILabel *subTitleLab_1 = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#3F4443",0.6) textAligment:NSTextAlignmentCenter];
    subTitleLab_1.text = title1.rh_localized;
    [itemView_1 addSubview:subTitleLab_1];
    [itemView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(itemWidth);
        make.left.mas_offset(0);
        make.top.mas_offset(0);
        make.height.offset(60);
        make.bottom.mas_offset(0);
    }];
    
    
    
    lab1.font = kBOLDSYSTEMFONT(14);
    lab1.textColor = kCOLOR_HEX(@"#3F4443");
    lab1.textAlignment = NSTextAlignmentCenter;
    [itemView_1 addSubview:lab1];
    
    [subTitleLab_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemView_1.mas_centerY).offset(3);
        make.width.lessThanOrEqualTo(@(itemWidth-20));
        make.centerX.mas_offset(0);
    }];
    
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView_1.mas_centerY).offset(-3);
    }];
    
    //线
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
    [itemView_1 addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(1);
        make.height.offset(35);
        make.centerY.mas_offset(0);
        make.right.equalTo(itemView_1);
    }];
    
    //右边第二个
    UIView *itemView_2 = kView;
    [itemBgView addSubview:itemView_2];
    QMUILabel *subTitleLab_2 = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#3F4443",0.6) textAligment:NSTextAlignmentCenter];
    subTitleLab_2.text = title2.rh_localized;
    [itemView_2 addSubview:subTitleLab_2];
    [itemView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(itemWidth);
        make.left.equalTo(itemView_1.mas_right);
        make.top.mas_offset(0);
        make.height.offset(60);
    }];
    
   
    lab2.font = kBOLDSYSTEMFONT(14);
    lab2.textColor = kCOLOR_HEX(@"#3F4443");
    lab2.textAlignment = NSTextAlignmentCenter;
    [itemView_2 addSubview:lab2];
    
    [subTitleLab_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.width.lessThanOrEqualTo(@(itemWidth-35));
        make.top.equalTo(itemView_2.mas_centerY).offset(3);
    }];
    
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView_2.mas_centerY).offset(-3);
    }];

       
    UIImageView *iconV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"plant_info_income"]];
    [itemView_2 addSubview:iconV];
    [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(16);
        make.centerY.equalTo(subTitleLab_2);
        make.left.equalTo(subTitleLab_2.mas_right).offset(2);
    }];
    
    return bgView;
}

-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}
@end
